package com.songshuai.webapi.jobapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.songshuai.webapi.jobapi.dto.base.QueryResponse;
import com.songshuai.webapi.jobapi.service.JobNetPostService;
import com.songshuai.webapi.jobapi.util.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * @Auther: Administrator
 * @Date: 2018/11/2 0002 16:26
 * @Description:
 */
@Component
@Slf4j
public class JobNetPostServiceImpl implements JobNetPostService {

    @Autowired
    RestTemplate restTemplate;

    @Value("${config.self.studycenter.key}")
    String studyCenterKey;

    @Value("${config.self.studycenter.host}")
    String studyCenterHost;

    @Value("${config.self.studycenter.appid}")
    String appId;

    @Override
    public QueryResponse<Map<String,Boolean>> PostFormData(Map<String,String> parms,String path) {
        QueryResponse<Map<String,Boolean>> resData=new QueryResponse<>();
        if(parms==null){
            parms=new HashMap<>();
        }
        parms.put("app_id",appId);
        parms.put("timestamp",String.valueOf(Util.timeStamp()));
        parms.put("rand",String.valueOf((int)(Math.random()*10000)));
        try {
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8");
            headers.setContentType(type);
            StringBuilder sBuilder=new StringBuilder();
            if(parms!=null){
                List<String>listKeys=new ArrayList<>(parms.keySet());
                Collections.sort(listKeys);
                for(String key:listKeys){
                    sBuilder.append(String.format("&%s=%s",key,parms.get(key)));
                }
                if (sBuilder.length() > 0) {
                    sBuilder.delete(0, 1);
                }
            }
            String encodingStr= Util.md5(sBuilder.toString()+studyCenterKey);
            //sBuilder.append("&sign=9cf0bd2af3361ebdcdb4d2133dda17c0");
            sBuilder.append("&sign="+encodingStr);
            String reqUrl = studyCenterHost +"/md_online/"+path;

            HttpEntity entity = new HttpEntity(sBuilder.toString(), headers);
            log.info(reqUrl+"?"+sBuilder.toString());
            //System.out.println(reqUrl+"?"+sBuilder.toString());
            ResponseEntity<String> res= restTemplate.exchange(reqUrl, HttpMethod.POST, entity, String.class);

            resData = JSON.parseObject(res.getBody(), new TypeReference<QueryResponse<Map<String,Boolean>>>() {});
            return  resData;
        } catch (Exception ex) {
            log.error("PostFormData error", ex);
        }
        return resData;
    }

    private HttpHeaders createHttpHeader() {
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.set("Accept-Charset", "utf-8");
        return requestHeaders;
    }
}
